package Server;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.Point;

import javax.swing.JButton;

import util.SocketManager;
import Client.Client;
import Client.clienteGUI;
import Client.frameAccion;

import javax.swing.JPopupMenu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.FlowLayout;


public class TablaConectados extends JFrame{
	private JTable tabla;
	public TablaConectados(ArrayList<infocon>info)
	{

		tabla = new JTable();
		JScrollPane scTabla= new JScrollPane();

		DefaultTableModel dtm = new DefaultTableModel(getFilas(info), getColumnas());
		tabla=new JTable(dtm){
			public boolean isCellEditable(int rowIndex, int vColIndex) {
				return false;
			}};

			tabla.setModel(dtm);
			scTabla.add(tabla);
			getContentPane().add(scTabla);
			this.setSize(303, 218);
			scTabla.setViewportView(tabla);			
			setTitle("Usuarios Conectados");
			JPanel panel = new JPanel();
			getContentPane().add(panel, BorderLayout.SOUTH);	

			class PopUp extends JPopupMenu {
				JMenuItem desconectaPop;

				public PopUp(){
					try{
						int index = tabla.getSelectedRow();
						String usuario=(String) tabla.getValueAt(index, 0);
						desconectaPop = new JMenuItem("Desconectar Usuario");
						add(desconectaPop);
						desconectaPop.addMouseListener(new MouseAdapter() {
							@Override
							public void mouseReleased(MouseEvent arg1) {
								try {
									WebServer.cerrarCon(usuario);
									WebServer.numc--;
									WebServer.disponible=WebServer.numc<WebServer.maxCon;
									WebServer.ig.setNumCon(Integer.toString(WebServer.numc));
									dtm.removeRow(index);
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}//solo debe recibir una linea					
							}
						});
					}catch(Exception e){
					}
				}
				private void doPop(MouseEvent e){
					PopUp menu = new PopUp();

					menu.show(e.getComponent(), e.getX(), e.getY());
				}
			}

			tabla.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent arg0) {
					if( arg0.getButton() ==3)
					{
						Point p = arg0.getPoint();
						int row=tabla.rowAtPoint(p);
						int colum = tabla.columnAtPoint(p);
						tabla.getSelectionModel().setSelectionInterval(row, row);
						PopUp pop= new PopUp();
						pop.doPop(arg0); 
					}
				}
			});

			JButton btnSalir = new JButton("Salir");
			btnSalir.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					dispose();
				}
			});
			panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

			JButton btnRecargar = new JButton("Recargar");
			btnRecargar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {			
					dispose();
					TablaConectados conectados = new TablaConectados(info);
					conectados.setVisible(true);
					//					DefaultTableModel dtm = new DefaultTableModel(getFilas(info), getColumnas());
					//					tabla.setModel(dtm);
				}
			});
			panel.add(btnRecargar);
			panel.add(btnSalir);
	}

	private String[][] getFilas(ArrayList<infocon> info)
	{
		String [][] filas = new String [info.size()][1];
		for(int i=0; i<info.size();i++){
			filas[i][0] = (info.get(i).getNombre());
		}

		return filas;
	}

	private String[] getColumnas()
	{
		String columna[]=new String[]{"NOMBRE DE USUARIO"};
		return columna;
	}



}